package com.dst.smart.socket.info;

import android.util.Log;

import com.dst.smart.app.MyApplication;
import com.dst.smart.tools.AppTools;
import com.dst.smart.tools.Base64;
import com.dst.smart.tools.JSONHelper;
import com.dst.smart.tools.SystemInfo;

import java.io.UnsupportedEncodingException;

public class ISocketCode {

	public static String setLogin(String masterName, String passWord) {
		Command info = new Command();
		info.setFromId(getMasterID());
		info.setName(masterName);
		info.setData(SystemInfo.getSSID(MyApplication.getInstance()));
		info.setToId("0000000000");
		info.setCommand("Login");
		info.setUser("admin");
		info.setPwd(passWord);
		info.setType(2);
		info.setResCode(-1);
		return  JSONHelper.toJSON(info);
	}

	public static String setLine(String masterName,String pwd) {
		Command info = new Command();
		info.setFromId(getMasterID());
		info.setName(masterName);
		info.setData(SystemInfo.getSSID(MyApplication.getInstance()));
		info.setToId("0000000000");
		info.setCommand("Line");
		info.setUser("admin");
		info.setPwd(pwd);
		info.setType(1);
		info.setResCode(-1);
		return  JSONHelper.toJSON(info);
	}

	public static String setDownInfo(String Data, String ToId) {
		return getResponse("DownInfo", ToId, Data);
	}

	public static String setMasterfo(String Data, String ToId) {
		return getResponse("GetMasterInfo", ToId, Data);
	}

	public static String setDownApk(String Data, String ToId) {
		return getResponse("ApkInfo", ToId, Data);
	}

	public static String setForward(String Data, String ToId) {
		return getResponse("Forward", Data, ToId);
	}

	public static String setResponse(String Data, String ToId) {
		return getResponse("Response", ToId, Data);
	}

	public static String setDeviceInfo(String Data, String ToId) {
		return getResponse("GetDeviceInfo", ToId, Data);
	}
	public static String setVoiceInfo(String Data, String ToId) {
		return getResponse("GetVoiceInfo", ToId, Data);
	}
	
	public static String setModeInfo(String Data, String ToId) {
		return getResponse("GetModeInfo", ToId, Data);
	}
	public static String setModeListInfo(String Data, String ToId) {
		return getResponse("GetModeList", ToId, Data);
	}

	public static String setAnHongInfo(String Data, String ToId) {
		return getResponse("GetAnHongInfo", ToId, Data);
	}

	public static String setCloudResponse(String Data, String ToId) {
		return getResponse("CloudResponse", ToId, Data);
	}
	public static String setGateWayInfo(String Data, String ToId) {
		return getResponse("GateWayInfo", ToId, Data);
	}
	public static String setSubscribeInfo(String Data, String ToId) {
		return getResponse("GetSubscribeInfo", ToId, Data);
	}
	
	public static String setPush(String Data, String ToId) {
		return getResponse("SetPush", ToId, Data);
	}

	public static String getError() {
		return "Error|00000|0000|0|00000|DST";
	}

	public static String getResponse(String Command, String ToId, String Data) {
		Command info = new Command();
		info.setFromId(getMasterID());
		info.setName("admin");
		info.setData(Data);
		info.setToId(ToId);
		info.setCommand(Command);
		info.setUser("admin");
		info.setPwd("admin");
		info.setType(1);
		info.setResCode(-1);
		String json = JSONHelper.toJSON(info);
		return json;
	}

	static String masterID = "";

	public static String getMasterID() {
		if (masterID.equals("")) {
			masterID = SystemInfo.getMasterID(MyApplication.getInstance());
		}
		return masterID;
	}
	public static byte[] getBuffer(String data){
		byte[] mBuffer=null;
		try {
			byte[]	buffer = data.getBytes("UTF-8");
			byte[] totalBuffer= AppTools.intToByteArray(buffer.length+4);
			byte[] dataBuffer=AppTools.intToByteArray(buffer.length);
			mBuffer=new byte[totalBuffer.length+dataBuffer.length+buffer.length];
			System.arraycopy(totalBuffer, 0, mBuffer, 0, totalBuffer.length);
			System.arraycopy(dataBuffer, 0, mBuffer, totalBuffer.length, dataBuffer.length);
			System.arraycopy(buffer, 0, mBuffer, totalBuffer.length+dataBuffer.length, buffer.length);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return mBuffer;
	}
}
